import 'package:dailoz/constants.dart';
import 'package:dailoz/data/page_data.dart';
import 'package:dailoz/service/homePage_service/homePage_service.dart';
import 'package:dailoz/size_config.dart';
import 'package:dailoz/view_model/home_page_view_model.dart';
import 'package:dailoz/widgets/page_title.dart';
import 'package:dailoz/widgets/progress_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:dailoz/generated/l10n.dart' as lang;

class HomePage extends StatefulWidget {
  const HomePage({
    Key? key,
  }) : super(key: key);

  static String id = "HomePageId";

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  HomePageViewModel? _homePageViewModel;

  @override
  void initState() {
    super.initState();
    _homePageViewModel = HomePageViewModel();
    _homePageViewModel!.fetchTodayTaskList();
    _homePageViewModel!.fetchStatusTask();
    _homePageViewModel!.fetchUserName();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => _homePageViewModel,
      child: Consumer<HomePageViewModel>(
        builder: (context, provider, _) {
          return ProgressWidget(
            loading: provider.isLoading,
            msg: lang.S.of(context).isLoading,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SizedBox(
                  width: SizeConfig.getProportionateScreenWidth(327.0),
                  child: Column(
                    children: <Widget>[
                      SizedBox(
                        height: SizeConfig.getProportionateScreenHeight(75.0),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              PageTitle(
                                title:
                                    "${lang.S.of(context).hi} ${provider.userName}",
                                textColor: homeColor,
                                fontSize:
                                    SizeConfig.getProportionateFontSize(28.0),
                              ),
                              SizedBox(
                                height: SizeConfig.getProportionateScreenHeight(
                                    9.0),
                              ),
                              PageTitle(
                                title: lang.S.of(context).homePageSlogan,
                                textColor: homeTextColor,
                                fontSize:
                                    SizeConfig.getProportionateFontSize(14.0),
                              ),
                            ],
                          ),
                          // GestureDetector(
                          //   onTap: () {},
                          //   child: CircleAvatar(
                          //     radius:
                          //         SizeConfig.getProportionateScreenHeight(20.0),
                          //     backgroundColor: Colors.white,
                          //     child: Center(
                          //       child: Image(
                          //         image: const AssetImage(
                          //             "assets/images/face.png"),
                          //         height:
                          //             SizeConfig.getProportionateScreenHeight(
                          //                 45.0),
                          //         width:
                          //             SizeConfig.getProportionateScreenHeight(
                          //                 45.0),
                          //       ),
                          //     ),
                          //   ),
                          // )
                        ],
                      ),
                      SizedBox(
                        height: SizeConfig.getProportionateScreenHeight(18.0),
                      ),
                      SizedBox(
                        height: SizeConfig.getProportionateScreenHeight(565.0),
                        child: RefreshIndicator(
                          onRefresh: () async {
                            provider.refresh();
                          },
                          child: SingleChildScrollView(
                            physics: const AlwaysScrollableScrollPhysics(),
                            child: Column(
                              children: <Widget>[
                                Row(
                                  children: <Widget>[
                                    PageTitle(
                                      title: lang.S.of(context).myTask,
                                      textColor: homeColor,
                                      fontSize:
                                          SizeConfig.getProportionateFontSize(
                                              24.0),
                                    ),
                                  ],
                                ),
                                SizedBox(
                                  height:
                                      SizeConfig.getProportionateScreenHeight(
                                          20.0),
                                ),
                                Row(
                                  children: HomePageService.generateStatusTask(
                                      provider.statusTaskList, context),
                                ),
                                Padding(
                                  padding: EdgeInsets.only(
                                    top:
                                        SizeConfig.getProportionateScreenHeight(
                                            15.0),
                                  ),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      PageTitle(
                                        title: lang.S.of(context).todayTask,
                                        textColor: homeColor,
                                        fontSize:
                                            SizeConfig.getProportionateFontSize(
                                                20.0),
                                      ),
                                      TextButton(
                                        onPressed: () {
                                          Provider.of<PageData>(context,
                                                  listen: false)
                                              .increment(1);
                                        },
                                        child: Text(
                                          lang.S.of(context).viewAll,
                                          style: Theme.of(context)
                                              .textTheme
                                              .bodyText2
                                              ?.copyWith(
                                                fontSize: SizeConfig
                                                    .getProportionateFontSize(
                                                        14.0),
                                              ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                HomePageService.generateTask(
                                    provider.todayTaskList, context),
                              ],
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
